package entity.domain;

import entity.domain.topic.ContinuousTopic;
import entity.domain.topic.DiscreteTopic;
import entity.domain.topic.Range;
import entity.domain.topic.Topic;
import entity.domain.value.DiscreteValue;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class DomainHelper {

    public static void addTopic(Domain domain, Topic topic){
        int amountOfTopics = domain.getTopics().size();
        topic.setNum(amountOfTopics);
        domain.getTopics().add(topic);
    }

    public static void removeTopicByTopicNum(Domain domain, int topicNum){
        int index = 0;
        int amountOfTopics = domain.getTopics().size();
        for(int i = 0; i<amountOfTopics; i++){
            if(domain.getTopics().get(i).getNum() == topicNum){
                index = i;
                break;
            }
        }
        domain.getTopics().remove(index);
        amountOfTopics = domain.getTopics().size();
        for(int i = index; i<amountOfTopics; i++){
            Topic topic = domain.getTopics().get(i);
            topic.setNum(topic.getNum() - 1);
        }
    }

    public static Domain loadDomainFromXML(String path) {

        Domain domain = new Domain();
        try{
            SAXReader reader = new SAXReader();
            Document doc = reader.read(new InputStreamReader(DomainHelper.class.getResourceAsStream(path)));
            Node node = doc.selectSingleNode("//name[1]");
            domain.setName(node.getText());
            node = doc.selectSingleNode("//description[1]");
            domain.setDescription(node.getText());
            List<Node> nodes = doc.selectNodes("//topic");
            List<Topic> topics = new ArrayList<>();
            for(Node item: nodes){
                Element element = (Element)item;
                Topic topic = null;
                if("discrete".equals(element.attributeValue("type"))){
                    List<Node> options = item.selectNodes("//option");
                    List<DiscreteValue> discreteValues = new ArrayList<>();
                    for(Node item2: options){
                        Element option = (Element)item2;
                        DiscreteValue discreteValue = new DiscreteValue(Integer.parseInt(option.attributeValue("num")), option.attributeValue("value"), option.attributeValue("description"));
                        discreteValues.add(discreteValue);
                    }
                    topic = new DiscreteTopic(Integer.parseInt(element.attributeValue("num")), element.attributeValue("name"), discreteValues ,element.attributeValue("description"));
                }else if("continuous".equals(element.attributeValue("type"))){
                    Element eleRange = (Element) item.selectSingleNode("//range");
                    Range range = new Range(Float.parseFloat(eleRange.attributeValue("lowerbound")), Float.parseFloat(eleRange.attributeValue("upperbound")));
                    topic = new ContinuousTopic(Integer.parseInt(element.attributeValue("num")), element.attributeValue("name"), range, element.attributeValue("description"));
                }
                topics.add(topic);
            }
            domain.setTopics(topics);
        }catch (DocumentException e){
            e.printStackTrace();
        }
        return domain;
    }

    public static void main(String[] args) {
        loadDomainFromXML("domain_xml/domain1/domain.xml");
    }
}
